<?php
/**
 * Created by PhpStorm.
 * User: peter
 * Date: 2018/6/15
 * Time: 下午2:46
 */

namespace app\model;


class Member extends Base
{
    protected $pk = 'uid';
    // 此处表名请务必设置为public
    public $table = 'member';

    /**
     * @purpose 管理微信授权表
     * @return \think\model\relation\HasOne
     */
    public function wechatInfo () {
        return $this
            ->hasOne('WeChat','uid', 'uid');
    }
    /**
     * @purpose 管理微信授权表
     * @return \think\model\relation\HasOne
     */
    public function wechat () {
        return $this
            ->hasOne('WeChat','uid', 'uid')
            ->bind('openid,unionid,subscribe,subscribe_scene');
    }
    /**
     * @purpose 关联当前模型查询
     * @return \think\model\relation\HasOne
     */
    public function parent () {
        return $this
            ->hasOne('Member','invitation', 'invitation_from')
            ->bind([
               'parent_nickname' => 'nickname'
            ]);
    }

    /**
     * @purpose 关联获取是否关注微信公众号
     * @return \think\model\relation\HasOne
     */
    public function subscribeByWechat () {
        return $this->hasOne('WeChat', 'uid', 'uid')
            ->bind('subscribe');
    }

    /**
     * @purpose 根据token获取用户信息
     * @return \think\model\relation\HasOne
     */
    public function tokenByInfo () {
        return $this->hasOne('MemberLoginToken', 'uid', 'uid');
    }
    /**
     * @purpose 通过条件获取数据条数
     * @param array $where
     * @return int
     */
    public function total(array $where = []):int
    {
        return (int) $this
            ->where($where)
            ->count();
    }
}